package max.cute.java8inaction.chap6;

import java.util.Comparator;
import java.util.DoubleSummaryStatistics;
import java.util.stream.Collectors;

import static max.cute.java8inaction.chap6.Dish.menu;
/**
 * 归约与汇总
 */
public class Summarizing {

    public static void main(String[] args) {
        counting();
        System.out.println("- - - - - - ");
        maxBy_MinBy();
        System.out.println("- - - - - - ");
        summing();
        System.out.println("- - - - - - ");
        summaryStatistics();
        System.out.println("- - - - - - ");
        joining();
        System.out.println("- - - - - - ");
    }

    /**
     * 计数
     */
    private static void counting() {
        // before java 8
        int howManyDishes = menu.size();

        // in java 8

        long howManyDishes_8_0 = menu.stream().count();
        long howManyDishes_8_1= menu.stream().collect(Collectors.counting());

        System.out.println(howManyDishes);
        System.out.println(howManyDishes_8_0);
        System.out.println(howManyDishes_8_1);
    }

    /**
     * 查找流中的最大值和最小值
     */
    private static void maxBy_MinBy() {
        // before java 8
        int maxCalories = Integer.MIN_VALUE;
        int minCalories = Integer.MAX_VALUE;
        for (Dish dish : menu) {
            int calories = dish.getCalories();
            if (calories > maxCalories) {
                maxCalories = calories;
            }
            if (calories < minCalories) {
                minCalories = calories;
            }
        }
        System.out.println(maxCalories);
        System.out.println(minCalories);

        // in java 8
        int maxCalories_8 = menu.stream().max(Comparator.comparing(Dish::getCalories)).get().getCalories();
        int minCalories_8 = menu.stream().min(Comparator.comparing(Dish::getCalories)).get().getCalories();

        System.out.println(maxCalories_8);
        System.out.println(minCalories_8);
    }

    /**
     * 求和/求平均值
     */
    private static void summing() {
        // sum
        // avg

        // before java 8
        double sum = 0;
        double avg;

        for (Dish dish : menu) {
            sum += dish.getCalories();
        }
        avg = sum / menu.size();

        System.out.println(sum);
        System.out.println(avg);

        // in java 8

        double sum_8 = menu.stream().collect(Collectors.summingDouble(Dish::getCalories));
        double avg_8 = menu.stream().collect(Collectors.averagingDouble(Dish::getCalories));
        System.out.println(sum_8);
        System.out.println(avg_8);
    }

    /**
     * 统计汇总
     */
    private static void summaryStatistics() {
        // count , sum , min , average, max
        // before java 8
        int count = menu.size();
        int maxCalories = Integer.MIN_VALUE;
        int minCalories = Integer.MAX_VALUE;
        double sum = 0;
        double avg;

        for (Dish dish : menu) {
            sum += dish.getCalories();
            int calories = dish.getCalories();
            if (calories > maxCalories) {
                maxCalories = calories;
            }
            if (calories < minCalories) {
                minCalories = calories;
            }
        }
        avg = sum / menu.size();
        System.out.println(count);
        System.out.println(sum);
        System.out.println(minCalories);
        System.out.println(avg);
        System.out.println(maxCalories);
        // in java 8
        DoubleSummaryStatistics doubleSummaryStatistics = menu.stream().collect(Collectors.summarizingDouble(Dish::getCalories));
        System.out.println(doubleSummaryStatistics);
    }

    /**
     * 连接字符串
     */
    private static void joining() {
        // before java 8
        StringBuffer stringBuffer = new StringBuffer();
        for (Dish dish : menu) {
            stringBuffer.append(dish.getName()).append(",");
        }
        System.out.println(stringBuffer.deleteCharAt(stringBuffer.length() - 1).toString());

        // in java 8
        String shortMenus = menu.stream().map(Dish::getName).collect(Collectors.joining(","));
        System.out.println(shortMenus);
    }

}
